package com.eco.fanliapp.admin.exception;

import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.service.IAppUserService;
import com.eco.fanliapp.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author xiaoxx
 * @Time 2019年6月3日上午10:34:45
 */
@ControllerAdvice
public class GlobalExceptionReslover extends BaseController {

    @Autowired
    private IAppUserService appUserService;

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionReslover.class);

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ServerResponse<String> errorHandler(Exception ex) {
        ex.printStackTrace();
        log.error("Exception异常类型{},具体异常:{}", ex.getStackTrace(), ex);
        return ServerResponse.createByErrorCodeMessage(500, "服务器错误");
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ServerResponse<String> runtimeExceptionHandler(RuntimeException ex) {
        ex.printStackTrace();
        log.error("RuntimeException异常类型{},具体异常:{}", ((ex == null) ? "RuntimeException异常类型--ex为空" : ex.getStackTrace()), ex);
        Throwable cause = ex.getCause();
        if (cause instanceof com.fasterxml.jackson.databind.exc.InvalidFormatException)
            return ServerResponse.createByErrorMessage("参数格式错误：请检查参数类型是否正确！");
        else if(ex instanceof NullPointerException ){
            //获取用户查询用户是否存在
            UserVO userVO = getAuthentication();
            if(!StringUtils.isEmpty(userVO)){
                AppUser appUser = appUserService.selectAppUserByUserId(userVO.getUserId());
                if (StringUtils.isEmpty(appUser)) {
                    return ServerResponse.createByErrorCodeMessage(505, "请重新登录");
                }
            }
            return ServerResponse.createByErrorCodeMessage(500, "服务器错误");
        }
        else
            return ServerResponse.createByErrorCodeMessage(500, "服务器错误");

    }

    @ExceptionHandler(value = UsernameIsExitedException.class)
    @ResponseBody
    public ServerResponse<String> UsernameIsExitedException(UsernameIsExitedException ex) {
        ex.printStackTrace();
        log.error("UsernameIsExitedException异常类型{},具体异常:{}", ex.fillInStackTrace(), ex.getStackTrace());
        return ServerResponse.createByErrorCodeMessage(500, "服务器错误");
    }

    @ExceptionHandler(value = TokenException.class)
    @ResponseBody
    public ServerResponse<String> TokenException(TokenException ex) {
        ex.printStackTrace();
        log.error("TokenException异常类型{},具体异常:{}", ex.fillInStackTrace(), ex.getStackTrace());
        return ServerResponse.createByErrorCodeMessage(500, "服务器错误");
    }

    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ServerResponse<String> errorHandler2(BaseException ex) {
        ex.printStackTrace();
        log.error("BaseException异常类型{},具体异常:{}", ex.fillInStackTrace(), ex.getStackTrace());
        return ServerResponse.createByErrorCodeMessage(500, "服务器错误");
    }
   /* private AppUser getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        try {
            AppUser appUser = JSONObject.parseObject((String)authentication.getPrincipal(),AppUser.class);
            return appUser;
        } catch (Exception e) {
            return null;
        }
    }*/

}
